package com.gopay.bis.service.bank.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gopay.bis.dao.bankorder.BankContractSerialInfoDAO;
import com.gopay.bis.manager.bank.BankAuthOrderManager;
import com.gopay.bis.service.bank.BankAuthOrderService;
import com.gopay.common.constants.bank.BankAuthOrderStatus;
import com.gopay.common.domain.bank.BankAuthOrder;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ExPayBaseMessage;
import com.gopay.common.domain.bis.BisBankContractSerialInfo;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.common.util.MD5Util;

/** 
 * 委托代收认证主订单Service实现
 * @ClassName: BankAuthOrderServiceImpl 
 * @author w_jiqing 
 * @date 2017年2月10日 下午1:21:14  
 */ 
@Service("bankAuthOrderService")
public class BankAuthOrderServiceImpl implements BankAuthOrderService{
    
    @Resource(name = "bankAuthOrderManager")
    private BankAuthOrderManager bankAuthOrderManager;
    
    @Resource(name = "bankContractSerialInfoDAO")
    private BankContractSerialInfoDAO bankContractSerialInfoDAO;

    @Override
    public BankAuthOrder addBankAuthOrder(BaseMessage base) throws GopayException {
        
        Date curDate = DateUtils.getSystemDate();
        BankAuthOrder authOrder = new BankAuthOrder();
        authOrder.setOrderId(base.getOrderId());
        authOrder.setBankBizProdCode(base.getBankBusProd().value);
        authOrder.setInterfaceChennelCode(base.getBankIntrChann().value);
        authOrder.setBankCode(base.getBank());
        authOrder.setOutOrderId(base.getOrderId());
        authOrder.setOrderStatus(BankAuthOrderStatus.AUTH_PROCESS);
        authOrder.setOrderType(base.getBankOrderType());
        authOrder.setTotalCheckTimes(0);
        authOrder.setCreatedTs(curDate);
        authOrder.setSendBankTm(curDate);
        authOrder.setLastUpdTs(curDate);
        authOrder.setLock(0);
        bankAuthOrderManager.addBankAuthOrder(authOrder);
        return authOrder;
    }

    @Override
    public int updateBackSuccess(BaseMessage base) throws GopayException{
        BankAuthOrder authOrder = new BankAuthOrder();
        authOrder.setOrderId(base.getOrderId());
        authOrder.setOrderStatus(BankAuthOrderStatus.AUTH_SUCCESS);
        int result = bankAuthOrderManager.updateBackSuccess(authOrder);
        return result;
    }

    @Override
    public int updateBackFail(BaseMessage base) throws GopayException{
        BankAuthOrder authOrder = new BankAuthOrder();
        authOrder.setOrderId(base.getOrderId());
        authOrder.setOrderStatus(BankAuthOrderStatus.AUTH_FAIL);
        int result = bankAuthOrderManager.updateBackFail(authOrder);
        return result;
    }

    @Override
    public int updateAutoQuerySuccess(BaseMessage base) throws GopayException{
        BankAuthOrder authOrder = new BankAuthOrder();
        authOrder.setOrderId(base.getOrderId());
        authOrder.setOrderStatus(BankAuthOrderStatus.AUTO_CHECK_SUCCESS);
        int result = bankAuthOrderManager.updateAutoQuerySuccess(authOrder);
        return result;
    }

    @Override
    public int updateBackVerifyFail(BaseMessage base) throws GopayException{
        BankAuthOrder authOrder = new BankAuthOrder();
        authOrder.setOrderId(base.getOrderId());
        authOrder.setOrderStatus(BankAuthOrderStatus.AUTH_VERIFY_FAIL);
        int result = bankAuthOrderManager.updateBackVerifyFail(authOrder);
        return result;
    }

    @Override
    public int updateAutoQueryFail(BaseMessage base) throws GopayException{
        BankAuthOrder authOrder = new BankAuthOrder();
        authOrder.setOrderId(base.getOrderId());
        authOrder.setOrderStatus(BankAuthOrderStatus.AUTO_CHECK_FAIL);
        int result = bankAuthOrderManager.updateAutoQueryFail(authOrder);
        return result;
    }

    @Override
    public boolean checkCardCer(ExPayBaseMessage base) throws GopayException{
        BisBankContractSerialInfo bcsi = new BisBankContractSerialInfo();
        bcsi.setBankCode(base.getBank());
//        bcsi.setBankBizProdCode(base.getBankBusProd().value);
        bcsi.setIntrfaceChannelCode(base.getBankIntrChann().value);
        bcsi.setCardNo(base.getCardNo());  
        bcsi.setContractDesc("certificate");
        String elementsValue = MD5Util.sign(base.getIdentityCode() + base.getCardHolder() + base.getCardNo() + base.getMediaId());
        BisBankContractSerialInfo bbcsi = bankContractSerialInfoDAO.getContractInfobyCard(bcsi);
        if(bbcsi != null && elementsValue.equals(bbcsi.getElementsValue())){
            return true;
        }
        return false;
    }

    @Override
    public BankAuthOrder addBankAuthOrderSuccess(BaseMessage base) throws GopayException {
                
        Date curDate = DateUtils.getSystemDate();
        BankAuthOrder authOrder = new BankAuthOrder();
        authOrder.setOrderId(base.getOrderId());
        authOrder.setBankBizProdCode(base.getBankBusProd().value);
        authOrder.setInterfaceChennelCode(base.getBankIntrChann().value);
        authOrder.setBankCode(base.getBank());
        authOrder.setOutOrderId(base.getOrderId());
        authOrder.setOrderStatus(BankAuthOrderStatus.AUTH_CACHE_SUCCESS);
        authOrder.setOrderType(base.getBankOrderType());
        authOrder.setTotalCheckTimes(0);
        authOrder.setCreatedTs(curDate);
        authOrder.setSendBankTm(curDate);
        authOrder.setLastUpdTs(curDate);
        authOrder.setLock(0);
        bankAuthOrderManager.addBankAuthOrder(authOrder);
        return authOrder;
    }

    @Override
    public int updateAuthBankOrderIdbyOrderId(BaseMessage base) throws GopayException {
        BankAuthOrder authOrder = new BankAuthOrder();
        authOrder.setOrderId(base.getOrderId());
        authOrder.setOutOrderId(base.getBankOrderId());
        int result=bankAuthOrderManager.updateAuthBankOrderIdbyOrderId(authOrder);
        return result;
    }

    @Override
    public BankAuthOrder getAuthBankOrderByBankId(String bankOrderId) {
        return bankAuthOrderManager.getAuthBankOrderByBankId(bankOrderId);
    }

}
